package com.imooc.authorization.controller;

import com.imooc.authorization.common.AjaxResult;
import com.imooc.authorization.common.tree.Node;
import com.imooc.authorization.common.tree.Tree;
import com.imooc.authorization.context.NativeCache;
import com.imooc.authorization.entity.Function;
import com.imooc.authorization.service.FunctionService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2016/8/23.
 */
@Controller
@RequestMapping("/function")
public class FunctionController {

    @Autowired private FunctionService functionService;
    @Autowired private NativeCache nativeCache;

    /**
     * 菜单首页
     * @return 首页的逻辑视图
     */
    @RequestMapping(value = "/index")
    public String menuList(){

        return "security/user/function_list";
    }

    /**
     * 新建、修改功能
     * @param function 功能对象
     * @return 操作结果
     */

    @RequestMapping(value = "/addEditFunction",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addEditFunction(Function function){
        if(null==function.getId()){
            function.setSerialNum(nativeCache.getFunctions().size());
            functionService.addFunction(function);
            nativeCache.addFunction(function);
        }else{
            functionService.updateUrl(function.getId(),function.getUrl());
            nativeCache.replaceFunction(function);
        }
        return AjaxResult.success();
    }

    /**
     * 根据ID删除功能信息
     * @param id 功能ID
     * @return 操作结果
     */

    @RequestMapping(value = "/deleteFunction",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult deleteFunctionById(Long id){
        functionService.deleteFunctionById(id);
        nativeCache.removeFunction(id);
        return AjaxResult.success();
    }

    /**
     * 果询子功能信息
     * @param page 当前页码
     * @param rows 每页记录数
     * @param parentId 父节点ID
     * @return 子功能信息集合
     */
    @RequestMapping(value = "/getSubFunctions",method = RequestMethod.POST)
    @ResponseBody
    public List<Function> getSubFunctions(Integer page,Integer rows,Long parentId){
        if(Objects.equals(null,parentId)){
            parentId=0L;
        }

        return functionService.getFunctions(page,rows,parentId);

    }

    /**
     * 构建用于新建、修改使用的菜单功能树
     * @return 包含父子关系的树形节点集合
     */
    @RequestMapping(value = "/buildFunctionTreeForEdit",method = RequestMethod.POST)
    @ResponseBody
    public List<Node> buildMenuTreeForEdit()
    {
        List<Function> list=nativeCache.getFunctions();
        Tree tree = new Tree(list);

        return tree.build();

    }


}
